home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr11
/
powerb5.zip
/
P5DOS006.TIP
< prev
next >
Wrap
Text File
|
1993-06-01
|
2KB
|
60 lines
The FOR statement makes DOS run the same command on each of
a list of files. For instance, you might type FOR %F IN
(*.DOC *.TXT) DO TYPE %F to display every DOC and TXT file
in the current directory. The trouble is that FOR can run
only one command on each of the listed files. If you want to
list and copy all the DOC and TXT files, forget it. FOR
doesn't let you run that kind of loop.
I've discovered a solution. You can create a batch file that
recursively calls itself and then jumps to a subroutine
within itself every time FOR acts on a file. An example,
FORTEST.BAT, is attached.
Michael Sigmundt
Willowdale, Ontario, Canada
Editor's Note: While the FOR command does allow you to
execute a CALL command to run multiple commands on each file
in a list, you normally need to establish a separate batch
file to be called. This technique lets you put everything in
one batch file. To see how it works, simply run FORTEST at
the DOS prompt. Note how the 'ECHO Once' and 'ECHO Twice'
commands (these substitute in this example for whatever real
commands you may need) work on each file in the directory.
This trick essentially allows you to implement a subroutine
-- something DOS's batch language can't normally do. (DR DOS
and most third-party shells can.) A special value of the
first parameter acts as a signal to the batch file, which
can jump to the subroutine code on entry. Note Mr.
Sigmundt's clever use of the SHIFT command to eliminate the
special parameter once the subroutine is entered.
FORTEST.BAT
---- BEGIN LISTING ----
@ECHO OFF
IF "%1"=="#LOOP#" GOTO LOOP
ECHO This is the beginning....
FOR %%F IN (*.*) DO CALL FORTEST.BAT #LOOP# %%F
ECHO This is the end....
GOTO END
:LOOP
REM Subroutine portion here
SHIFT
ECHO Once with %1
ECHO Twice with %1
:END
---- END LISTING ----
Title: Batch Files that Loop the Loop
Category: DOS
Issue Date: August, 1992
Editor: Brett Glass
Supplementary Files: None
Filename: P5DOS006.TIP